/*
 * Copyright 2008 Slava Chernyak
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package com.chernyak.DataAdapter.types;

import java.util.List;
import java.util.Map;

import com.chernyak.DataAdapter.CompilerException;
import com.chernyak.DataAdapter.GlobalSymbolProperties;

public class CompositeTypeDecl extends TypeDecl {
	String type;
	
	public CompositeTypeDecl(String type, String name, int ln) {
		super(name, false, ln);	
		this.type = type;
	}

	@Override
	public void lowerDeclarations(StringBuilder sink) {
		sink.append("public " + type + " " + name + " = null;");

	}

	@Override
	public void lowerEquals(StringBuilder sink) {
		// TODO: generate check for null
		sink.append("__da_test &= " + this.name + ".equals(__da_o);");
	}
	
	@Override
	public void typecheck(Map<String, GlobalSymbolProperties> globalSymbolTable,
			List<String> parrentSymbolTable, String packageContext) {
		super.typecheck(globalSymbolTable, parrentSymbolTable, packageContext);
		
		String fullType = (packageContext != null) ? packageContext + "." + type : type;
		for(String decl : globalSymbolTable.keySet()) {			
			if (fullType != null && fullType.equals(decl)) {
				return;
			}
		}
		
		throw new CompilerException("Type " + fullType + " is undeclared.", ln);
	}

	public void lowerComparisonWCG(StringBuilder sink) {
		// TODO: generate compare on composite if comparable
	}
	
	public void lowerHashCode(StringBuilder sink) {
		if (compare) {
			sink.append("__da_hashcode += (null != " + name + ") ? " + name + ".hashCode(): -1;");
		}
	}
	
	public void lowerToString(StringBuilder sink) {
		lowerToString(sink,0);
	}
	
	public void lowerParse(StringBuilder sink) {
		throw new CompilerException("Parsing not implemented on composite types", ln);
	}
	
	protected void lowerToString(StringBuilder sink, int arrayDim) {
		sink.append("__da_str += ((" + name + " != null) ? " + name +  
				genArrayDeref(arrayDim) + ".toString() : \"null\" ) + __DA_STRING_DELIM;");
	}
	
	public void lowerReadWCG(StringBuilder sink) {
		lowerRead(sink, 0);
	}

	public void lowerWriteWCG(StringBuilder sink) {
		lowerWrite(sink, 0);
	}

	protected void lowerRead(StringBuilder sink, int arrayDim) {
		sink.append("if (d.readBoolean() == false) {");
		sink.append(	name + genArrayDeref(arrayDim) + " = null;");
		sink.append("} else {");
		sink.append(	name + genArrayDeref(arrayDim) + " = new " + type + "();");
		sink.append(	name + genArrayDeref(arrayDim) + ".readFields(d);");
		sink.append("}");
	}

	protected void lowerWrite(StringBuilder sink, int arrayDim) {
		sink.append("if (" + name + genArrayDeref(arrayDim) + " == null) {");
		sink.append(	"d.writeBoolean(false);");
		sink.append("} else {");
		sink.append(	"d.writeBoolean(true);");
		sink.append(	name + genArrayDeref(arrayDim) + ".write(d);");
		sink.append("}");
	}
	
	protected String getTypeName() {
		return "type";
	}
}
